package com.example.jobback.websocket.controller;

import com.example.jobback.websocket.config.WebSocketConfigurator;
import com.example.jobback.websocket.service.MessageService;
import jakarta.websocket.*;
import jakarta.websocket.server.ServerEndpoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.io.IOException;
/**
 * @ClassName: MessageEndpointController
 * @Description: websocket端点
 * @Author: lhb
 * @Date: 2025/5/24
 */
@ServerEndpoint(value = "/message/{role}",configurator = WebSocketConfigurator.class)
@Component
@CrossOrigin
public class MessageEndpointController {
    private static MessageService messageService;


    @Autowired
    public void setMessageService(MessageService messageService) {
        MessageEndpointController.messageService=messageService;
    }

    @OnOpen
    public void onOpen(Session session, EndpointConfig config) throws IOException {
        messageService.onOpen(session,config);
    }

    @OnMessage(maxMessageSize = 10485760)
    public void onMessage(String message,Session SendSession) throws IOException {
        messageService.onMessage(message,SendSession);
    }

    @OnClose
    public void onClose(Session session) throws IOException {
        messageService.onClose(session);
    }

    @OnError
    public void onError(Session session, Throwable throwable) {
        System.out.println("Error occurred in session: " + session.getId());
        throwable.printStackTrace();
    }
}
